package com.ny.controller;

import com.ny.entity.Result;
import com.ny.entity.User;
import com.ny.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/selectAllUser")
    public Result selectAllUser(){
        return Result.success(userService.findAllUser());
    }
    // 登录:http://localhost:8081/login?username=admin&password=<PASSWORD>

    @GetMapping("/selectALLUser")
    public Result selectALLUser(){
        return Result.success(userService.findAllUser());
    }

    @PostMapping("/login")
    public Result findUser(String username, String password){
        User user = userService.login(username,password);
        if (username != null&&user!=null){
            return Result.success(user);
        }
        return Result.notFound("用户不存在");
    }
    //    根据用户名查询用户信息
//    http://localhost:8081/findUserByUsername?username=admin
    @GetMapping("/findUserByUsername")
    public Result findUserByUsername(String username){
        User user = userService.findUserByUsername(username);
        System.out.println(user);
        if (username != null&&user!=null)  {
            return Result.success(user);
        }
        return Result.notFound("用户不存在");
    }
    // 注册
    @PostMapping("/registerUser")
    public Result registerUser(User user){
        return Result.success(userService.registerUser(user));
    }

//    @PostMapping("/sendEmailCode")
//    public Result sendEmailCode(String email) {
//        userService.sendEmailCode(email);
//        return Result.success();
//    }

    //http://localhost:8081/findUserByEmail?email=<EMAIL>
    @GetMapping("/findUserByEmail")
    public Result findUserByEmail(String email) {
        User user = userService.findUserByEmail(email);
        if (user != null) {
            return Result.success(user);
        }
        return Result.notFound("邮箱号不存在");
    }

    @GetMapping("/sendEmailCode")
    public Result sendEmailCode(String email){
        User user = userService.findUserByEmail(email);
        if (user!= null){
            userService.sendEmailCode(email);
            return Result.success("发送成功");
        }
        return Result.notFound("邮箱号不存在");
    }

    @PutMapping("/updatePasswordByEmail")
    public Result updatePasswordByEmail(String email, String password,String code){
        Boolean checkCode =userService.checkCode(email, code);
        if (checkCode){
            int i =userService.updatePasswordByEmail(email, password);
            if (i>0){
                return Result.success("修改成功");
            }
            return Result.notFound("成功获取验证码");
        }
        else {
            return Result.success("验证码错误");
        }

    }


}
